<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class GamesTagsController extends AppController {

    var $name = 'GamesTags';
    var $components = array(
        'Session', 'Alias'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array(
        'Gametags'
    );

    function index(){
        $this->set('title_for_layout', Configure::read('site_name') . ' - Tags');
        $tag_id = array_key_exists('tag_id', $this->params['named']) ? $this->params['named']['tag_id'] : 0;
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search
        if ($search) {
            $conditions['OR'] = array(
                'Game.title LIKE' => "%$search%",
                'Game.id LIKE' => "%$search%",
            );
        }
        $conditions['AND']  =  array(
            'tag_id'    =>  $tag_id
        );
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Gametags.ordering' => 'ASC'
            ),
        );
        $data = $this->paginate('Gametags');
        $this->set('tag_id', $tag_id);
        $this->set('total', $this->params['paging']['Gametags']['count']);
        $this->set('data', $data);
        $this->set('total_inpage', sizeof($data));
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Gametags->alias}.{$this->Gametags->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        if ($this->Gametags->deleteAll($conditions, true)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d nội dung", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index', 'tag_id' => $this->params['form']['tag_id']));
    }
}